package org.chunchu.todo;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Provider;

import org.chunchu.todo.model.Task;
import org.chunchu.todo.mvp.impl.TaskList;
import org.chunchu.todo.mvp.impl.TaskListPresenter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class TaskListAdapter extends BaseAdapter implements TaskList {

	private static final String TAG_POS = "pos";

	private TaskListPresenter presenter;

	@Inject
	private static Provider<Context> contextProvider;

	private LayoutInflater layoutInflater;

	private List<Task> tasks;

	@Inject
	public TaskListAdapter(TaskListPresenter presenter) {
		layoutInflater = LayoutInflater.from(contextProvider.get());
		this.presenter = presenter;
		this.presenter.initializeWith(this);
	}

	@Override
	public int getCount() {
		return tasks.size();
	}

	@Override
	public Object getItem(int position) {
		return tasks.get(position);
	}

	@Override
	public List<Task> getTasks() {
		return tasks;
	}

	@Override
	public Task getTaskAt(int position) {
		return (!tasks.isEmpty()) ? tasks.get(position) : null;
	}

	@Override
	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
		notifyDataSetChanged();
	}

	@Override
	public long getItemId(int position) {
		return tasks.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup groupView) {

		Bundle posBundle = new Bundle();
		posBundle.putInt(TAG_POS, position);
		convertView = layoutInflater.inflate(R.layout.task_item, null);

		TextView tvTaskName = (TextView) convertView.findViewById(R.id.name);
		TextView tvTaskDetail = (TextView) convertView
				.findViewById(R.id.detail);
		Button btnEditTask = (Button) convertView
				.findViewById(R.id.editTaskItem);
		Button btnRemoveTask = (Button) convertView
				.findViewById(R.id.removeTaskItem);

		tvTaskName.setOnClickListener(clickListerner);
		tvTaskName.setTag(posBundle);
		tvTaskDetail.setOnClickListener(clickListerner);
		tvTaskDetail.setTag(posBundle);
		btnEditTask.setTag(posBundle);
		btnRemoveTask.setTag(posBundle);

		btnEditTask.setOnClickListener(editListerner);
		btnRemoveTask.setOnClickListener(removeListerner);

		tvTaskName.setText(tasks.get(position).getName());
		tvTaskDetail.setText(tasks.get(position).getDetail());
		return convertView;
	}

	private final OnClickListener clickListerner = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Bundle bundle = (Bundle) v.getTag();
			Integer position = bundle.getInt(TAG_POS);
			presenter.changePosition(position);
		}
	};

	private final OnClickListener removeListerner = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Bundle bundle = (Bundle) v.getTag();
			Integer position = bundle.getInt(TAG_POS);
			presenter.removeItem(position);
		}
	};

	private final OnClickListener editListerner = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Bundle bundle = (Bundle) v.getTag();
			Integer position = bundle.getInt(TAG_POS);
			presenter.editItem(position);
		}
	};

	@Override
	public void updateUi() {
		notifyDataSetChanged();
	}
}
